<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\customer\model;

use think\Model;

/**
 * 多店铺客服系统
 * 客服管理
 * @author 晓风<215628355@qq.com>
 * @date 2021-5-26 9:17:56
 */
class Customer extends Model {

    // 设置当前模型对应的完整数据表名称
    protected $table = '__CUSTOMER__';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
 

 //设置主键
    protected $pk = "aid";    
    
    const MAX = 100;

    public static $status =[
        0 =>"休息",
        1 =>"在线",
        2 =>"忙碌"
    ];


    /**
     * 获取文档列表
     * @param mixed $map 筛选条件
     * @param mixed $order 排序
     * @return mixed
     * @author 晓风<215628355@qq.com>
     */
    public static function getList($map = [], $order = []) {
        return self::view("customer")
                    ->view("user","mobile,user_nickname,head_img","customer.member_id = user.id","left")
                    ->view("customer_shop","store_name,store_logo","customer.store_id = customer_shop.store_id")
                    ->where($map)
                    ->order($order)
                    ->paginate();
    }
    
    /**
     * 获取我的店铺清单
     * @param mixed $map 筛选条件
     * @param mixed $order 排序
     * @return mixed
     * @author 晓风<215628355@qq.com>
     */
    public static function getStores($user_id) {
        return self::view("customer")
                    ->view("customer_shop","store_name,store_logo","customer.store_id = customer_shop.store_id")
                    ->where("customer.member_id",$user_id)
                    ->select();
    }
      /**
     * 检测门店客服数量
     * @author 晓风<215628355@qq.com>
     * @param int $store_id
     * @return type
     */
    public static function checkUsercount(int $store_id){
        $count = self::getUsercount($store_id);
        return $count < self::MAX  ? true : false;
    }
    /**
     * 获取门店客服数量
     * @author 晓风<215628355@qq.com>
     * @param int $store_id
     * @return type
     */
    public static function getUsercount(int $store_id){
        return self::where("store_id",$store_id)->count();
    }
    /**
     * 检测有无绑定客服
     * @author 晓风<215628355@qq.com>
     * @param int $store_id
     * @param int $user_id
     * @return type
     */
    public static function checkBind(int $store_id ,int $user_id){
        return self::where("store_id",$store_id)->where("member_id",$user_id)->find();
    }
    /**
     * 绑定客服到门店
     * @author 晓风<215628355@qq.com>
     * @param int $store_id 店铺ID
     * @param int $user_id 会员ID
     * @return self
     * @throws \Exception
     */    
    public static function bindUser(int $store_id,int $user_id){ 
        $shop = \app\customer\model\Shop::where("store_id",$store_id)->find();
        if(!$shop){
            throw new \Exception("该店铺ID还未注册，请先注册店铺");
        }
        $check = self::checkUsercount($shop['store_id']);
        if(!$check){
            throw new \Exception("店铺绑定客服已满");
        }        
        $res =  self::checkBind($store_id,$user_id);
        $isBind = 1;
        if(!$res ){
            $res = self::create([
                "store_id"=>$store_id,
                "member_id"=>$user_id,
            ]);
            $isBind = 2;
        }    
        if(!$res){
            throw new \Exception("绑定客服失败");
        }    
        return $isBind;
    }
    /**
     * 获取本地全部已连线客服以及其状态
     * @author 晓风<215628355@qq.com>
     * @param int $store_id
     */
    public static function getLineServices(int $store_id){       
        $list = self::where("store_id",$store_id)->column("status","member_id");
        $redis = \app\common\model\Redis::handler();
        foreach($list as $id=>$status){
           //判断客服是否连接socket 
           $is_line = $redis->hget("service_user_list",$id);
           if(!$is_line){
                $list[$id] = $status = 0; //状态值设置为0 离线
           }       
        }
        return $list;
    }


    /**
     * 从数据包中随机抽取一个可用客服
     * @author 晓风<215628355@qq.com>
     * @param array $services 全部客服数据
     */
    public static function getServiceDataToRandId($services){      
        if(!$services){
            return 0;
        }   
        //找出在线客服
        $okUsers = array_filter($services,function($status){
            return $status == 1 ? true :  false;
        });
        if($okUsers){
            return array_rand($okUsers);
        }
        //找出可用客服
        $okUsers = array_filter($services,function($status){
            return $status != 0 ? true :  false;
        });
        if($okUsers){
            return array_rand($okUsers);
        }
        return 0;        
    }
     
    /**
     * 随机分配一个可用客服(用户)
     * @author 晓风<215628355@qq.com>
     * @param int $store_id 门店ID
     */
    public static function getRandServiceId(int $store_id){     
        $services = self::where("store_id",$store_id)->column("status","user_id");
        return self::getServiceDataToRandId($services);        
    }
    
     /**
     * 设置客服状态
     * @author 晓风<215628355@qq.com>
     * @param int $store_id
     * @param int $user_id
     * @return type
     */
    public static function setStatus(int $store_id ,int $user_id , int $status){
        return self::where("store_id",$store_id)->where("member_id",$user_id)->setField("status",$status);
    }
}
